plugin Helper FiliTools_exportMesh
name:"Export Mesh" 
classID:#(0x7d5df729, 0x2cfe118)
category:"FiliTools" 
extends:dummy 
( 

local meshObj

parameters p_basic rollout:rl_basic
(
	obj_target		type:#node
	obj_name		type:#string ui:ed_nameObj
	obj_fileName	type:#string default:""
	type_binary		type:#boolean ui:cb_binary default:true
	export_skin		type:#boolean ui:cb_skin default:true
	
	on obj_target set obj do
	(
		if obj != undefined then
		(
			obj_name = obj.name
		)
		else
		(
			obj_name = "undefined"
		)
	)
)

parameters p_anim rollout:rl_anim
(
	anm_fileName	type:#string default:""
	anm_list		type:#string default:"Default=0|Idle=|Walk=|Run=|Attack1=|Attack2=|Attack3=|Defend=|Hurt=|Death=|Special1=|Special2=|Special3=|Special4=5,6,9,4,7"
)

rollout rl_basic "Basic"
(
	fn filter_geometry o = (Superclassof o == Geometryclass and classof o != TargetObject ) --any geometry but no TargetObject (it has no mesh)

	pickbutton btn_pickObj "..." pos:[134,25] width:21 height:21 filter:filter_geometry
	editText ed_nameObj "" pos:[8,25] width:121 height:21 enabled:false

	button btn_saveMesh "Save mesh" pos:[40,101] width:82 height:21
	label lbl_target "Target object" pos:[14,7] width:140 height:15
	checkbox cb_binary "Binary mode" pos:[8,51] width:121 height:20 checked:true
	checkbox cb_skin "Export skin" pos:[8,73] width:121 height:20 checked:true

	on btn_pickObj picked obj do
	( 
		if obj != undefined do
		(
			obj.wirecolor = red
			obj_target = obj
		)
	)
	on btn_saveMesh pressed do
	(
		if (obj_target == undefined) do
		(
			MessageBox "No object selected"
			return 0
		)

		if (type_binary) then
		(
			ftools_export_type = FTOOLS_TYPE_FIXED16
		)
		else
		(
			ftools_export_type = FTOOLS_TYPE_TEXT
		)
		
		fname = getSaveFileName caption:"Save mesh" filename:obj_fileName types:"Mesh (*.msh)|*.msh|All files (*.*)|(*.*)|"
		if (fname != undefined) do
		(
			obj_fileName = fname
			ftools_exportObject obj_target true fname
		)
	)
)

rollout rl_anim "Animation"
(
	fn splitText text sep =
	(
		items = #()
			
		if (text == undefined) do
			return items
			
		last = 0
		for i = 1 to text.count do
		(
			if text[i] == sep do
			(
				s = substring text (last + 1) (i-last-1)
				if s.count>0 do
					append items s
				last = i
			)
		)
		
		s = substring text (last+1) (text.count - last)
		if s.count>0 do
			append items s
		
		return items
	)
	
	fn fillTreeFromString tv =
	(
		root = tv.Nodes.add()
		root.text = "ROOT"
		
		items = splitText anm_list "|"
		
		
		for i = 1 to items.count do
		(
			anim = splitText items[i] "="
			frames = splitText anim[2] ","
								
			itm = tv.Nodes.add root.index 4 "" (anim[1] + " [" + (frames.count as string) + "]") 0
			
			for j = 1 to frames.count do
			(
				frm = tv.Nodes.add itm.index 4 "" frames[j] 0
			)
		)
		
		root.expanded = true
	)	
	
	ActiveXControl tv_anims "MSComctlLib.TreeCtrl" pos:[8,8] width:148 height:168

	button btn_saveAnim "Save anim" pos:[40,262] width:82 height:20
	
	
	on btn_saveAnim pressed do
	(
		if (obj_target == undefined) do
		(
			MessageBox "No object selected"
			return 0
		)

		if (type_binary) then
		(
			ftools_export_type = FTOOLS_TYPE_FIXED16
		)
		else
		(
			ftools_export_type = FTOOLS_TYPE_TEXT
		)
		
		fname = getSaveFileName caption:"Save anim" filename:anm_fileName types:"Anim (*.anm)|*.anm|All files (*.*)|(*.*)|"
		if (fname != undefined) do
		(
			anm_fileName = fname
		)
	)
	
	on rl_anim open do
	(
		tv_anims.Indentation = 28*15
		tv_anims.LineStyle = #tvwRootLines
		tv_anims.hideSelection = false
		
		fillTreeFromString tv_anims
	)
	
	on tv_anims MouseDown Button Shift x y do 
		format "Button: %, Shift: %, xy: %,%\n" Button Shift x y

)

on getDisplayMesh do
(
	if (meshObj == undefined) do 
	(
		meshObj = createInstance box length:10 \
		width:10 height:10 mapCoords:false \
	)	
	meshObj.mesh
)

tool create 
( 
	on mousePoint click do 
	(
		nodeTM.translation = gridPoint;#stop 
	)
)
)